<?php
session_start();
if(!isset($_SESSION['myusername'])){
header("location:login.php");die;
}
else
{
require("pais.php");
require ("conf.php");
$yhendus=new mysqli($host, $username, $password, $db_name);
if (isset($_REQUEST["makstud"])&isset($_SESSION['myusername']))
{
	$kask=$yhendus->prepare("Update m2ngurid SET makstud=1 WHERE id=?");
	$kask->bind_param("i", $_REQUEST["makstud"]);
	$kask->execute();
	$yhendus->close();
	header("Location: $_SERVER[PHP_SELF]");
	
}
if (isset($_REQUEST["avalikusta"]))
{
	$kask=$yhendus->prepare("UPDATE m2ngurid SET avalik=1 WHERE id=?");
	$kask->bind_param("i", $_REQUEST["avalikusta"]);
	$kask->execute();
	$yhendus->close();
	header("Location: $_SERVER[PHP_SELF]");
}

$kask=$yhendus->prepare("SELECT id, nick, eesnimi, perenimi, email, makstud, avalik FROM m2ngurid ORDER BY makstud");
$kask->bind_result($id,$nick,$eesnimi,$perenimi,$email, $makstud, $avalik);
$kask->execute();
echo"<table border='1'>
<tr>
<th>ID</th>
<th>Nick</th>
<th>Eesnimi</th>
<th>Perekonnanimi</th>
<th>Email</th>
<th>Makstud</th>
</tr>
";

while($kask->fetch()){
	if($makstud==0)
	{
		$maks="Ei";
	}
	else if($makstud==1) {
		$maks="Jah";
	}
	echo"
	<tr>
		<td>$id</td>
		<td>$nick</td>
		<td>$eesnimi</td>
		<td>$perenimi</td>
		<td>$email</td>
		<td>$maks</td>";
		if($makstud==0)
		{
		echo"<td><a href='?makstud=$id'>Makstud</a></td>";
		}
		if($avalik==0)
		{
		echo"<td><a href='?avalikusta=$id'>Muuda avalikuks</a></td>";
		}
	echo"<tr>";
}
echo"</table></br>";
$yhendus->close();
echo"<a href='logout.php'>Logi välja</a>";
require("jalus.php");
}
?>